package com.itboye.sunsun.adapter;

import android.app.ActionBar.LayoutParams;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;

import android.text.SpannableString;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import com.itboye.sunsun.R;
import com.sunsun.chatemoji.ChatMsgEntity;
import com.sunsun.chatemoji.FaceConversionUtil;

/**
 * 
 ******************************************
 * @文件名称	:  ChatMsgAdapter.java
 * @文件描述	: 消息数据填充起
 ******************************************
 */
public class ChatMsgAdapter extends BaseAdapter {
	private RelativeLayout huati_ream;
	private PopupWindow mWindow;// 消息弹出
	private int screenWidth = 0;// window弹出宽度

	public static interface IMsgViewType {
		int IMVT_COM_MSG = 0;
		int IMVT_TO_MSG = 1;
	}

	private List<ChatMsgEntity> coll;
	private LayoutInflater mInflater;
	private Context context;
	public ChatMsgAdapter(Context context, List<ChatMsgEntity> coll) {
		this.coll = coll;
		mInflater = LayoutInflater.from(context);
		this.context = context;
	}

	public int getCount() {
		return coll.size();
	}

	public Object getItem(int position) {
		return coll.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public int getItemViewType(int position) {
		ChatMsgEntity entity = coll.get(position);

		if (entity.getMsgType()) {
			return IMsgViewType.IMVT_COM_MSG;
		} else {
			return IMsgViewType.IMVT_TO_MSG;
		}

	}

	public int getViewTypeCount() {
		return 2;
	}

	public View getView(int position, View convertView, ViewGroup parent) {

		ChatMsgEntity entity = coll.get(position);
		boolean isComMsg = entity.getMsgType();

		ViewHolder viewHolder = null;
		if (convertView == null) {
			
			
			
		
				convertView = mInflater.inflate(
						R.layout.chatting_item_msg_text_right, null);
			

			viewHolder = new ViewHolder();
			viewHolder.tvSendTime = (TextView) convertView
					.findViewById(R.id.tv_sendtime);
			viewHolder.tvContent = (TextView) convertView
					.findViewById(R.id.tv_chatcontent);
			viewHolder.img_chat_pin=(ImageView) convertView.findViewById(R.id.img_chat_pin);
			viewHolder.isComMsg = isComMsg;

			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		viewHolder.img_chat_pin.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				v = LayoutInflater.from(context).inflate(
						R.layout.pupdindow, null);
				mWindow = new PopupWindow(v, screenWidth, LayoutParams.FILL_PARENT);
				mWindow.setFocusable(true);
				mWindow.setBackgroundDrawable(new BitmapDrawable());
				mWindow.setOutsideTouchable(true);
				System.out.println(screenWidth + ":宽高：" + 0);
//				v.findViewById(R.id.text_aquarium);
				mWindow.showAsDropDown(v, 0, 0);
				Toast.makeText(context, "wer", 0).show();
				
			}
		});
		huati_ream=(RelativeLayout) convertView.findViewById(R.id.huati_ream);
		huati_ream.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Toast.makeText(context, "", 0).show();
			}
		});
		viewHolder.tvSendTime.setText(entity.getUseip());
		SpannableString spannableString = FaceConversionUtil.getInstace().getExpressionString(context, entity.getMessage());
		viewHolder.tvContent.setText(spannableString);

		return convertView;
	}

	

	class ViewHolder {
		public TextView tvSendTime;
		public TextView tvContent;
		public  ImageView img_chat_pin;
		public boolean isComMsg = true;
	}

	
	public void onMessagePupuwindow() {
//		Toast.makeText(context, "we", 0).show();
		
	}
}
